let app = getApp();
let API_URL = app.globalData.API_URL ;
import { getPickUpDetail } from "../../../../request/api.js";
import dayjs from "../../../../common/js/dayjs.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        postId: 0,
        isLike: false,
        detailed: {}
    },

    /**
     * My Function
     */
    //  获取内容
    getPost() {
        let postId = this.data.postId
         let data = { postId }
         getPickUpDetail(res => {
            if(res.statusCode == 200) {
                let time = res.data
                if (time.favorite != null) {
                    this.setData({ isLike: true })
                }
                time.releaseTime = dayjs(time.releaseTime).format("YYYY-MM-DD")
                this.setData({ detailed: res.data })
            }
        }, data);
    },

    // 收藏
    onLike() {
        let user_id = wx.getStorageSync('uid')
        if (!user_id) {
            app.login()
            return
        }
        let data = { target_id: parseInt(this.data.postId, 10), type: 7 }

        if(this.data.isLike) {
            let data = { postId: this.data.detailed.favorite.id }
            wx.request({
                url: `${API_URL}/favoritesDestroy`,
                data: data,
                header: app.header(),
                method: 'post',
                success: res=> {
                    if(res.statusCode == 200) {
                        this.setData({ isLike: false })
                        wx.showToast({
                            title: 'ok',
                            duration: 2000
                        })
                    }
                }
            })
        } else {
            wx.request({
                url: `${API_URL}/favorites`,
                data: data,
                header: app.header(),
                method: 'post',
                success: res=> {
                    if(res.statusCode == 200) {
                        let favorite = {
                            id : res.data.insertId
                        }
                        let detailed = this.data.detailed
                        detailed.favorite = favorite
                        this.setData({ isLike: true, detailed: detailed })
                        wx.showToast({
                            title: 'ok',
                            duration: 2000
                        })
                    }
                }
            })
        }
    },

    // 打电话联系
    onContact() {
        wx.makePhoneCall({
            phoneNumber: this.data.detailed.phoneNum,
            fail: cancel => {
                console.log("cancel: ", cancel);
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "pickedUpInfo", navigation: true, postId: options.itemId })
        this.getPost()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 朋友圈分享
     */
    onShareTimeline: function () {
        return {
            title: `${ this.data.lang.dir == "rtl" ? this.data.detailed.name_ug : this.data.detailed.name_zh }`,
            path: '/pages/home/pickedUp/pikedUpDetailed/pikedUpDetailed?postId=' + this.data.postId
        }
    }
})